/*     */ package com.newcrm.oauth2.huhoo;
/*     */ 
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.exception.OauthException;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import net.sf.json.JSONArray;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.jsoup.Connection;
/*     */ import org.jsoup.Connection.Method;
/*     */ import org.jsoup.Connection.Response;
/*     */ import org.jsoup.Jsoup;
/*     */ 
/*     */ public class HuhooClient
/*     */ {
/*     */   private static final String AUTHORIZE_URL = "http://open.huhoo.com/oauth2/authorize?client_id=";
/*     */   private static final String ACCESSTOKEN_URL = "http://open.huhoo.com/oauth2/access_token";
/*     */   private static final String TOKENINFO_URL = "http://open.huhoo.com/oauth2/token_info";
/*     */   private static final String COMPANYBASEINFO_URL = "http://open.huhoo.com/api/v1/company/baseinfo";
/*     */   private static final String COMPANYWORKER_URL = "http://open.huhoo.com/api/v1/company/worker";
/*  28 */   private String callbackUrl = null;
/*  29 */   private String appkey = null;
/*  30 */   private String secret = null;
/*     */ 
/*     */   public HuhooClient(String callbackUrl, String appKey, String secret) {
/*  33 */     this.callbackUrl = callbackUrl;
/*  34 */     this.appkey = appKey;
/*  35 */     this.secret = secret;
/*     */   }
/*     */ 
/*     */   public String loginUrl()
/*     */   {
/*  48 */     StringBuffer url = new StringBuffer("http://open.huhoo.com/oauth2/authorize?client_id=");
/*  49 */     url.append(this.appkey).append("&redirect_uri=")
/*  50 */       .append(this.callbackUrl).append("&response_type=code");
/*  51 */     return url.toString();
/*     */   }
/*     */ 
/*     */   public String getAccessToken(String code)
/*     */     throws OauthException
/*     */   {
/*     */     try
/*     */     {
/*  70 */       Connection.Response res = 
/*  71 */         Jsoup.connect("http://open.huhoo.com/oauth2/access_token")
/*  72 */         .data(new String[] { 
/*  72 */         "client_id", this.appkey, "client_secret", 
/*  73 */         this.secret, "grant_type", "authorization_code", 
/*  74 */         "code", code, "redirect_uri", this.callbackUrl })
/*  75 */         .method(Connection.Method.POST).timeout(10000).execute();
/*  76 */       String body = res.body();
/*     */ 
/*  78 */       JSONObject json = JSONObject.fromObject(body);
/*     */ 
/*  80 */       String result = json.getString("code");
/*  81 */       if (!"0".equals(result)) {
/*  82 */         throw new OauthException(json.getString("text"));
/*     */       }
/*  84 */       return json.getJSONObject("ext").getString("access_token");
/*     */     } catch (IOException e) {
/*  86 */       throw new OauthException(e.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getUID(String token)
/*     */     throws OauthException
/*     */   {
/*     */     try
/*     */     {
/*  99 */       Connection.Response res = Jsoup.connect("http://open.huhoo.com/oauth2/token_info")
/* 100 */         .data("access_token", token).method(Connection.Method.POST)
/* 101 */         .timeout(10000).execute();
/* 102 */       String body = res.body();
/*     */ 
/* 104 */       JSONObject json = JSONObject.fromObject(body);
/*     */ 
/* 106 */       String result = json.getString("code");
/* 107 */       if (!"0".equals(result)) {
/* 108 */         throw new OauthException(json.getString("text"));
/*     */       }
/* 110 */       return json.getJSONObject("ext").getString("uid");
/*     */     } catch (IOException e) {
/* 112 */       throw new OauthException(e.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public User getUserInfo(String token, String cid) throws OauthException {
/*     */     try {
/* 118 */       StringBuffer url = new StringBuffer("http://open.huhoo.com/api/v1/company/worker");
/* 119 */       url.append("?cid=").append(cid).append("&access_token=")
/* 120 */         .append(token);
/*     */ 
/* 122 */       Connection.Response res = Jsoup.connect(url.toString()).method(Connection.Method.GET)
/* 123 */         .timeout(10000).execute();
/* 124 */       String body = res.body();
/*     */ 
/* 126 */       JSONObject json = JSONObject.fromObject(body);
/*     */ 
/* 128 */       String result = json.getString("code");
/* 129 */       if (!"0".equals(result)) {
/* 130 */         throw new OauthException(json.getString("text"));
/*     */       }
/*     */ 
/* 133 */       User u = new User();
/* 134 */       u.isAdmin = json.getJSONObject("ext").getBoolean("is_admin");
/* 135 */       u.name = json.getJSONObject("ext").getString("worker_name");
/* 136 */       u.email = json.getJSONObject("ext").getString("worker_email");
/* 137 */       if ((u.email == null) || (u.email.trim().length() == 0)) {
/* 138 */         u.email = json.getJSONObject("ext").getString("worker_account");
/*     */       }
/* 140 */       return u;
/*     */     } catch (IOException e) {
/* 142 */       throw new OauthException(e.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getCompanyName(String cid, String token)
/*     */     throws OauthException
/*     */   {
/* 156 */     StringBuffer url = new StringBuffer("http://open.huhoo.com/api/v1/company/baseinfo");
/*     */ 
/* 158 */     url.append("?cid=").append(cid).append("&access_token=").append(token);
/*     */     try
/*     */     {
/* 161 */       Connection.Response res = Jsoup.connect(url.toString()).method(Connection.Method.GET)
/* 162 */         .timeout(10000).execute();
/*     */ 
/* 164 */       String body = res.body();
/* 165 */       JSONObject json = JSONObject.fromObject(body);
/*     */ 
/* 167 */       String result = json.getString("code");
/* 168 */       if (!"0".equals(result)) {
/* 169 */         throw new OauthException(json.getString("text"));
/*     */       }
/* 171 */       return json.getJSONObject("ext").getString("company_name");
/*     */     } catch (IOException e) {
/* 173 */       throw new OauthException(e.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 187 */     String body = "{\"code\":0,\"text\":null,\"ext\":[{\"headpic\": \"http://res.huhoo.cn/a/none.jpg\",\"nickname\": \"虎虎用户\"}]}";
/* 188 */     JSONObject json = JSONObject.fromObject(body);
/*     */ 
/* 190 */     String code = json.getString("code");
/*     */ 
/* 192 */     System.out.println(code);
/*     */ 
/* 194 */     if ("0".equals(code))
/*     */     {
/* 197 */       System.out.println(json.getJSONArray("ext").getJSONObject(0)
/* 198 */         .getString("nickname"));
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.oauth2.huhoo.HuhooClient
 * JD-Core Version:    0.6.1
 */